﻿using LituoPro.ltcore.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LituoPro.ltcore.service
{
    public class ReportService
    {

        private static RetItemEntity[] retItemEntities = {

            new RetItemEntity("startByte",0,1,0),
            new RetItemEntity("dataLengthByte",1,2,1),
            new RetItemEntity("verByte",2,1,3),     

            new RetItemEntity("tableTypeByte",3,1,4),  
            new RetItemEntity("pageFlagByte",4,1,5), 
            new RetItemEntity("channelByte",5,1,6),

            new RetItemEntity("cpuIdByte",6,16,7),
            new RetItemEntity("cmdUuidByte",7,8,23),    
            new RetItemEntity("dataByte",8,66,31),
  

            new RetItemEntity("uuidByte",9,8,97), 
            new RetItemEntity("verifyByte",10,1,105),
            new RetItemEntity("endByte",11,1,106)
        };


        private static RetItemEntity[] dataByteItemEntities = {

            new RetItemEntity("dataLengthByte",0,2,0),
            new RetItemEntity("claInsByte",1,2,2),
            new RetItemEntity("swByte",2,2,4),

            new RetItemEntity("p1p2Byte",3,2,6),
            new RetItemEntity("eventIdByte",4,4,8),
            new RetItemEntity("dateTimeByte",5,7,12),

            new RetItemEntity("aiCountByte",6,1,19),
            new RetItemEntity("aiByte",7,12,20),
            new RetItemEntity("diCountByte",8,1,32),

            new RetItemEntity("diByte",9,10,33),
            new RetItemEntity("a0FeedbackCountByte",10,1,43),
            new RetItemEntity("a0FeedbackByte",11,8,44),

            new RetItemEntity("a0CountByte",12,1,52),
            new RetItemEntity("a0Byte",13,8,53),
            new RetItemEntity("d0CountByte",14,1,61),


            new RetItemEntity("d0Byte",15,4,62),
        };


        public RetDataEntity convert(Byte[] bytes)
        {
            try
            {
                RetDataEntity retDataEntity = new RetDataEntity();
                for (int i = 0; i < retItemEntities.Length; i++)
                {
                    string fieldName = retItemEntities[i].ItemName;
                    int startIndex = retItemEntities[i].DataIndex;
                    int length = retItemEntities[i].DataLength;
                    byte[] newArray = bytes.Skip(startIndex).Take(length).ToArray();
                    if (!"dataByte".Equals(fieldName))
                    {
                        Type retDataEntityType = typeof(RetDataEntity);
                        PropertyInfo propertyInfo = retDataEntityType.GetProperty(fieldName);
                        propertyInfo.SetValue(retDataEntity, newArray);
                    }
                    else
                    {
                        DataByte dataByte = this.convertRetItemData(newArray);
                        Type retDataEntityType = typeof(RetDataEntity);
                        PropertyInfo propertyInfo = retDataEntityType.GetProperty(fieldName);
                        propertyInfo.SetValue(retDataEntity, dataByte);

                    }
                }
                return retDataEntity;
            }
            catch (Exception e) {
                return null;
            }
        }


        public DataByte convertRetItemData(Byte[] bytes)
        {
            try
            {
                DataByte dataByte = new DataByte();
                for (int i = 0; i < dataByteItemEntities.Length; i++)
                {
                    string fieldName = dataByteItemEntities[i].ItemName;
                    if ("d0Byte".Equals(fieldName))
                    {
                        int startIndex = dataByteItemEntities[i].DataIndex;
                        int length = dataByteItemEntities[i].DataLength;
                        byte[] newArray = bytes.Skip(startIndex).Take(length).ToArray();
                        Type retDataByteType = typeof(DataByte);
                        PropertyInfo propertyInfo = retDataByteType.GetProperty(fieldName);
                        propertyInfo.SetValue(dataByte, newArray);
                    }
                    else 
                    {
                        int startIndex = dataByteItemEntities[i].DataIndex;
                        int length = dataByteItemEntities[i].DataLength;
                        byte[] newArray = bytes.Skip(startIndex).Take(length).ToArray();
                        Type retDataByteType = typeof(DataByte);
                        PropertyInfo propertyInfo = retDataByteType.GetProperty(fieldName);
                        propertyInfo.SetValue(dataByte, newArray);
                    }
                }
                return dataByte;
            }
            catch (Exception e)
            {
                return null;
            }
        }





    }
}
